//
//  BDShopJSLIstViewController.m
//  iOS_beboy
//
//  Created by wen on 2017/9/26.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDDiscoverShopJSLIstViewController.h"
#import "BDShopJSCell.h"
#import "BDUserModel.h"
#import "BDUserInfoViewController.h"
#import "BDShopJSModel.h"
#import "BDPhotoGroupView.h"

@interface BDDiscoverShopJSLIstViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation BDDiscoverShopJSLIstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"技师列表");
    _datasource = [NSMutableArray array];
    [self requestDatasource];

}

- (void)requestDatasource{
    NSString *url = [NSString stringWithFormat:@"%@shops/%@/girls?page=%@&size=%@", KBDBaseURL, _shop_id, self.startPage, self.pageSize];
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        if ([_tableView.mj_header isRefreshing]) {
            [_datasource removeAllObjects];
            [_tableView.mj_header endRefreshing];
        }
        [_datasource addObjectsFromArray:[BDShopJSModel mj_objectArrayWithKeyValuesArray:x[@"list"]]];
        [self.tableView reloadData];
    }];
    [signal subscribeError:^(NSError * _Nullable error) {
        [self showErrorViewWithTapBlock:^{
            [self requestDatasource];
        }];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 87 + 26 + 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDShopJSCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopJSCell" forIndexPath:indexPath];
    BDShopJSModel *jsModel = _datasource[indexPath.row];
    [cell configeWithModel:jsModel avatarClick:^{
        UIView *fromView = cell.imageView;
        
        BDPhotoGroupItem *item = [BDPhotoGroupItem new];
        item.thumbView = cell.imageView;
        
        NSArray *items = @[item];
        
        BDPhotoGroupView *v = [[BDPhotoGroupView alloc] initWithGroupItems:items];
        [v presentFromImageView:fromView toContainer:[UIApplication sharedApplication].keyWindow animated:YES completion:nil];
    }];
    if (indexPath.row == 0) {
        cell.topLine.hidden = NO;
    }else{
        cell.topLine.hidden = YES;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BDShopJSModel *jsModel = _datasource[indexPath.row];
    if (jsModel.user_id != 0) {
        BDUserInfoViewController *userInfoVC = [[BDUserInfoViewController alloc] initWithUserID:jsModel.user_id isFollow:NO guanzhuBlock:nil];
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight - kBDButtonHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BDShopJSCell class] forCellReuseIdentifier:@"shopJSCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 10)];
        headView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = headView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view insertSubview:_tableView atIndex:0];
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
